var showEleDetail;
var $editDialog;
var corpId;
var orgId;
require(['grid', 'topBar', 'layout', 'date'], function () {
	var postData,rcx,type,orgIds;
	$('#top').topBar({
		left: [{
			type: 'filter-more',
			dateType: 'custom',
			search: function () {
				search();
			},
			item: [
				{
					text: '时间快捷',
					name1: 'beginTime',
					name2: 'endTime',
					type: 'dateLine'
				},
				{
					text: '司机姓名',
					name: 'drvname',
					width: '140',
					type: 'text'
				},
				{
					text: '发出站',
					name: 'startstopname',
					width: '140',
					type: 'text'
				},
				{
					text: '终点站',
					name: 'destinationname',
					width: '140',
					type: 'text'
				}
			]
		}
		],
		right: [
			{
				text: '导出',
				type: 'export-btn',
				authority: '',
				item: [
					{
						text: 'Excel导出',
						click: function () {
							exportFile('Excel');
						}
					},
					{
						text: 'PDF导出',
						click: function () {
							exportFile('Pdf');
						}
					}
				]
			},
			{
				text: '导入',
				type: 'btn',
				width: '80',
				click: function () {
					type = 'CAR';
					var orgIds = commonFun.getLeftTreeCheckedNodes(type,true);
					var data=commonFun.getLeftTreeCheckedNodes('LINE',true);
					if(orgIds == null || !orgIds.length || data==undefined){
						$.messager.alert("提示", "请选择企业,目前只支持珍宝和晋江!", "warning");
						return false;
					}
					corpId=data[0].getParentNode().corpId;
					if(corpId=='8650bf67257a49fc9bdcebec6555ac44' || corpId=='24243e69b1fe46c9af843470822c74fa'  ){
						
						getEditDialog().dialog('exec', 'accept',corpId,orgId);;
					}else{
						 $.messager.alert("提示", "目前只支持珍宝和晋江!", "warning");
						 return false;
					 }
				}
			}
		]
	});

	//获取左侧选择的节点信息
	function getPostData() {
		rcx = commonFun.getTreeCheckNodeToArgs();
		type = commonFun.getLeftTreeCurrentTreeType();
		orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if (rcx) {
			postData = $.extend(rcx, $("#top").parent().getValues());
			postData.type = type;
			postData.orgIds = orgIds;
		}
		return rcx;
	}

	function exportFile(suffix) {
		if (getPostData()) {
			if ($('#grid').getGridParam("reccount") < 1) {
				$.messager.alert("提示", "请查询数据。", "info");
				return;
			}
			var url = "/eleDetail/exportEleDetail" + suffix + ".htm";
			$.exportFile(url, postData);
		}
		if ($('#grid').getGridParam("reccount") > 0) {
			if (getPostData()) {
				var url = "/eleDetail/exportEleDetail" + suffix + ".htm";
				$.exportFile(url, postData);
			}
		}
	}
	function gridInit() {
		$('#grid').grid({
			multiselect: false,
			groupHeader:true,
			url: "/eleDetail/searchEleDetail.htm",
			colModel: [
				{name: "rundate", label: "营运日期"},
				{name: "licPltNo", label: "车牌号"},
				{name: "carInCd", label: "车辆编号", width: 80},
				{name: "drivename", label: "司机姓名"},
				{name: "startstopcode", label: "发出站编码"},
				{name: "destinationcode", label: "终点站编码"},
				{name: "startstopname", label: "发出站"},
				{name: "destinationname", label: "终点站"},
				{name: "task", label: "任务"},
				{name: "dispatchstarttime", label: "发出时间"},
				{name: "endtriptime", label: "到对站时间"},
				{name: "departinterval", label: "生成时间"},
				{name: "anchortime", label: "时间戳"},
				{name: "runtime", label: "运行时间"},
				{name: "runmileage", label: "里程", align: "right"},
				{name: "mileageoutofservice", label: "空程", align: "right"},
				{name: "remark", label: "备注"},
				{name: "rundirection", label: "方向"},
				{name: "yunMinTime", label: "最早数据时间"},
				{name: "yunMaxTime", label: "最早数据时间"},
				{name: "yunRngMile", label: "里程值"}
			]
		});
		
		$('#grid').grid('setGroupHeaders', {
			useColSpanStyle: true,
			groupHeaders: [
			{
				startColumnName: 'yunMinTime',
				numberOfColumns: 3,
				titleText: "<div class='t-header'>云总线数据</div>"
			}]
		});
	}

	function search() {
		if (getPostData()) {
			$('#grid').grid('setGridParam', {
				page: 1,
				datatype: "json",
				postData: postData
			}).trigger("reloadGrid");
		}
	}

	getEditDialog=function(fn){
		
        var data=commonFun.getLeftTreeCheckedNodes('LINE',true);
		 corpId=data[0].getParentNode().corpId;
		 orgId=data[0].getParentNode().id;
			 if(!$editDialog){
		        $editDialog = $('<div>').dialog({
		        	title: '导入电子更纸',
		            url: './energy/report/elePaper.htm',
		            height: 400,
		            width: 600,
		            autoOpen: true,
		            buttons: [
	                {
	                    text: '关闭',
	                    css: 'btn-cancel',
	                    click: function () {
	                        $(this).dialog('close');
	                    }
	                }
	            ]
		    });	
		}
			 $editDialog.dialog('open');
				return $editDialog;
	};
	
	
	
	showEleDetail = function (data) {
		//切换成车辆
		commonFun.showLeftTree(commonFun.carTree);
		//勾选并展开节点
		commonFun.setCheckedNodes(data.carIds);
		$('.topBar').find('[name=beginTime]').val(data.beginTime);
		$('.topBar').find('[name=endTime]').val(data.endTime);
		$('#grid').grid('setGridParam', {
			page: 1,
			datatype: "json",
			postData: data
		}).trigger("reloadGrid");
	}

	$('body').layout();
	//初始化grid
	gridInit();
});